import Network from './network'

export const getBanner = () => Network.get('banner?type=2')

export const getPersonalized = () => Network.get('personalized')

export const getSongList = (id) => Network.get('playlist/detail', id)

export const getHotArtists = () => Network.get('top/artists?offset=0&limit=10')

export const getLetterArtists = (letter) => {
  return new Promise((resolve, reject) => {
    Network.all([
      Network.get(`artist/list?offset=0&limit=5&cat=1001&initial=${letter}`),
      Network.get(`artist/list?offset=0&limit=5&cat=1002&initial=${letter}`),
      Network.get(`artist/list?offset=0&limit=5&cat=1003&initial=${letter}`)
    ])
      .then(result => {
        resolve(result)
      })
      .catch(err => {
        reject(err)
      })
  })
}
export const getAllArtists = (letter) => {
  return new Promise((resolve, reject) => {
    const keys = ['热']
    const list = [getHotArtists()]
    for (let i = 65; i <= 90; i++) {
      const char = String.fromCharCode(i)
      keys.push(char)
      list.push(getLetterArtists(char))
    }
    Network.all(list)
      .then(result => {
        const arr = []
        for (let i = 0; i < result.length; i++) {
          if (i === 0) {
            const hotObj = {}
            hotObj.title = keys[i]
            const hotItemArr = []
            result[i].data.artists.forEach(item => {
              const obj = {}
              obj.id = item.id
              obj.name = item.name
              obj.picUrl = item.picUrl + '?param=375y375'
              hotItemArr.push(obj)
            })
            hotObj.items = hotItemArr
            arr.push(hotObj)
          } else {
            const obj = {}
            obj.title = keys[i]
            const singerArr = []
            result[i].forEach(item => {
              item.data.artists.forEach(singer => {
                const obj = {}
                obj.id = singer.id
                obj.name = singer.name
                obj.picUrl = singer.picUrl + '?param=375y375'
                singerArr.push(obj)
              })
            })
            obj.items = singerArr
            arr.push(obj)
          }
        }
        resolve(arr)
      })
      .catch(error => {
        reject(error)
      })
  })
}

export const getSingerDetail = (singerId) => Network.get('artists', singerId)
export const getSongsUrls = (songsId) => Network.get('song/url', songsId)
//  Cannot read property 'lyric' of undefined at eval(有些没有歌词会报错?)
export const getSongLyric = (songId) => Network.get('lyric', songId)

export const getTopList = () => Network.get('toplist/detail')
export const getRankDetailList = (id) => Network.get('top/list', id)

export const getSearchList = (data) => Network.get('search', data)
export const getHotKey = () => Network.get('search/hot')
